const Router = require('koa-router');
const {db, Op,QueryTypes } = require("../../models/db");
const tool = require("../../tools/tool");
const router = new Router();

// 汇总明细
router.post('/app/getSaleGpDetail',async ctx => {
    let user = ctx.user
    let tenantId= user.tenantId
    let body = ctx.request.body;
    let beginDate= body.beginDate;
    let endDate= body.endDate;
    let gpType= body.gpType;
    let salesName= body.salesName;
    if(!salesName) {
        salesName = user.name
    }
    let buName= body.name;
    let invName= body.name;
    let sql = `select a.buName,a.billNo,a.billDate,a.buId,a.totalAmountReal,b.amountReal,b.invName,b.invId,b.spec,b.qty,b.unit,b.unit1,b.unit2 ,b.amount 
                         from v_main a inner join v_main_info b on a.tenantId=b.tenantId and a.id=b.iid where 
                         a.tenantId=${tenantId} and a.transType=150601 and a.billState='accounted' and a.billDate between '${beginDate}' and '${endDate}' 
                         `
    if(gpType === '商品汇总') {
        if(invName) {
            sql += ` and b.invName='${invName}'  and a.salesName='${salesName}' `
            let list = await db.query(sql,{type: QueryTypes.SELECT})
            return ctx.suc('', list)
        }
    }
    if(gpType === '客户汇总') {
        if(buName) {
            sql += ` and a.buName='${buName}'  and a.salesName='${salesName}' `
            let list = await db.query(sql,{type: QueryTypes.SELECT})
            return ctx.suc('', list)
        }
    }
    return ctx.suc('', [])
})

module.exports = router;
